﻿
#region 引用
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using SiteCommon;



#endregion

public partial class CustomerManage_CallManage : System.Web.UI.Page
{
    #region 字段
    private string _callPhone = "";
    private string _callPositionNo = "";
    private string _callOrderId = "";
    private int _callRecordId = 0;
    #endregion

    #region 页面初始化程序
    /// <summary>
    /// 页面初始化程序
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this._callPhone = SiteCommon.Common.GetString(Request.QueryString["CallPhone"]);
        this._callPositionNo = SiteCommon.Common.GetString(Request.QueryString["CallPositionNo"]);
        this._callOrderId = SiteCommon.Common.GetString(Request.QueryString["CallOrderId"]);
        this._callRecordId = SiteCommon.Common.GetInt(Request.QueryString["CallRecordId"]);
        
        lblError.Text = "";

        if (!this.IsPostBack)
        {
            this.GetPagingCallRecordList();  //当前用户来电记录列表
            Init_AttentionCategory(); //关注分类类型
            this.BindRecordType(); //来电类型
            this.UserInfoInit();

            if (this._callPhone != "")
            {
                txtCallPhone.Text = this._callPhone;
                txtCallPhone.Enabled = false;
            }
        }

    }
    #endregion

    #region 绑定当前页客户来电记录列表
    /// <summary>
    /// 绑定当前页客户来电记录列表
    /// </summary>
    private void GetPagingCallRecordList()
    {
        List<Model.PhoneRecord> list = null;
        int recordCount = 0;

        BLL.PhoneRecordBL mInter = new BLL.PhoneRecordBL();
        string ret = mInter.GetPagingRecordList(CutPageBar1.PageIndex,
            CutPageBar1.PageSize,
            this._callPhone,
            out list,
            out recordCount);

        CutPageBar1.RecordCount = recordCount;

        if (ret == Model.Config.SUCCESS)
        {
            rpList.Visible = true;
            rpList.DataSource = list;
            rpList.DataBind();
        }
        else
        {
            rpList.Visible = false;
            lblError.Text = ret;
        }
    }
    #endregion

    #region 绑定来电类型
    /// <summary>
    /// 绑定来电类型
    /// </summary>
    private void BindRecordType()
    {
        List<Model.RECORDTYPE> list = null;

        BLL.RecordTypeBL mInter = new BLL.RecordTypeBL();
        string ret = mInter.GetUsingTypeList(out list,"BYZD1=0");
        if (ret == Model.Config.SUCCESS)
        {
            ddlRecordType.DataTextField = "RT_Name";
            ddlRecordType.DataValueField = "RT_ID";
            ddlRecordType.DataSource = list;
            ddlRecordType.DataBind();

            DDL_DL.DataTextField = "RT_Name";
            DDL_DL.DataValueField = "RT_ID";
            DDL_DL.DataSource = list;
            DDL_DL.DataBind();

        }
        else
        {
            lblError.Text = ret;
        }

        ddlRecordType.Items.Insert(0, new ListItem("请选择来电类型", "0"));
        DDL_DL.Items.Insert(0, new ListItem("请选择来电类型", "0"));
    }

    #endregion

    #region 初始化用户信息
    /// <summary>
    /// 初始化用户信息
    /// </summary>
    private void UserInfoInit()
    {
        if (_callRecordId > 0)
        {
            Model.PhoneRecord model = new Model.PhoneRecord();
            model = new BLL.PhoneRecord().GetModel(_callRecordId);
            ddlRecordType.SelectedValue = model.RD_TYPE.ToString();
            txtUserCode.Text = model.RD_USERCODE;
            txtUserName.Text = model.RD_NAME;
            txtEmail.Text = model.Email;
            txtTel.Text = model.Tel;
            txtMobilePhone.Text = model.MobilePhone;
            txtAddress.Text = model.Address;
            if (model.BYZD3.Length > 0)
            {
                DDLAttentionCategory.SelectedValue = model.BYZD3;
            }
            Tbx_AttentionCategory.Text = model.BYZD4;
            if (model.BYZD2 > 0)
            {
                DDLOperateType.SelectedValue = model.BYZD2.ToString();
            }
            txtOperater.Text = model.RD_OPERATE;
            if (model.BYZD1 > 0)
            {
                CKB_GuanLian.Checked = model.BYZD1 == 1 ? true : false;            
            }
            TextBox1.Text = model.BYZD6;
            if (model.BYZD5 != "2")
            {
                DDStatus.SelectedValue = model.BYZD5;
            }

        }
        else
        {
            BLL.UserInfoBL mInter = new BLL.UserInfoBL();
            Model.V_CUSTOMER user = null;
            string ret = mInter.GetUserByCallPhone(this._callPhone, out user);
            if (ret == Model.Config.SUCCESS)
            {
                if (user != null)
                {
                    txtUserCode.Text = user.DM;
                    txtUserName.Text = user.GKMC;
                    txtEmail.Text = user.EMAIL;
                    txtTel.Text = user.DH;
                    txtMobilePhone.Text = user.SJ;
                    txtAddress.Text = user.DZ;
                }
            }
            txtOperater.Text = _callPositionNo;
        }
    }
    #endregion

    #region 添加来电记录
    /// <summary>
    /// 添加来电记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (this._callRecordId != 0)
        {
            lblError.Text = "来电记录已经添加";
            return;
        }

        Model.PhoneRecord record = new Model.PhoneRecord();
        record.Address = txtAddress.Text.Trim();
        record.CallOrderId = this._callOrderId;
        record.CallPhone = txtCallPhone.Text.Trim();
        if (_callPositionNo == "")
        {
            if (DDLOperateType.SelectedValue == "0")
            {
                record.CallPositionNo = txtOperater.Text.Trim();
            }
        }
        else
        {
            record.CallPositionNo = this._callPositionNo;
        }
        record.Email = txtEmail.Text.Trim();
        record.MobilePhone = txtMobilePhone.Text.Trim();
        record.RD_NAME = txtUserName.Text.Trim();
        record.RD_OPERATE = txtOperater.Text.Trim();
        record.RD_TYPE = int.Parse(ddlRecordType.SelectedValue);
        record.RD_USERCODE = txtUserCode.Text.Trim();
        record.Tel = txtTel.Text.Trim();
        record.BYZD3 = DDLAttentionCategory.SelectedValue;  //关注分类
        record.BYZD4 = Tbx_AttentionCategory.Text.Trim();   //关注分类内容
        record.BYZD1 = CKB_GuanLian.Checked == true ? 1 : 0;//是否关联通话
        record.BYZD2 = int.Parse(DDLOperateType.SelectedValue);//操作人员类型
        record.BYZD5 = DDStatus.SelectedValue; //处理情况
        record.BYZD6 = TextBox1.Text.Trim();   //关联通话编号
        if (DDStatus.SelectedValue == "0")
        {
            record.BYZD7 = DateTime.Now.ToString();
        }
        int recordId = 0;
        bool flag = false;
        if (!txtCallPhone.Enabled)
        {
            flag = true;
        }
        BLL.PhoneRecordBL mInter = new BLL.PhoneRecordBL();
        string ret = mInter.Add(record,flag, out recordId);
        if (ret == Model.Config.SUCCESS)
        {
            if (recordId > 0)
            {
                string url = "CustomerService.aspx?Phone=" + record.CallPhone
                    + "&DNO=" + record.CallPositionNo
                    + "&DID=" + record.CallOrderId
                    + "&CallRecordId=" + recordId
                    + "&ReturnUrl=CallManage.aspx"
                    + "&AOrderId=a3";

                string script = "<script>top.location.href='" + url + "';</script>";
                Response.Write(script);
                Response.End();
            }
            else
            {
                lblError.Text = "添加失败";
            }
        }
        else
        {
            lblError.Text = ret;
        }

    }
    #endregion

    #region 清空数据
    /// <summary>
    /// 清空数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnClear_Click(object sender, EventArgs e)
    {
        if (txtCallPhone.Enabled)
        {
            txtCallPhone.Text = "";
        }
        ddlRecordType.SelectedIndex = 0;
        txtUserCode.Text = "";
        txtUserName.Text = "";
        txtEmail.Text = "";
        txtTel.Text = "";
        txtMobilePhone.Text = "";
        txtAddress.Text = "";
        txtOperater.Text = "";
    }
    #endregion

    #region 初始化关注分类
    private void Init_AttentionCategory()
    {
        List<Model.RECORDTYPE> list = null;

        BLL.RecordTypeBL mInter = new BLL.RecordTypeBL();
        string ret = mInter.GetUsingTypeList(out list, "BYZD1=1");
        if (ret == Model.Config.SUCCESS)
        {
            DDLAttentionCategory.DataTextField = "RT_Name";
            DDLAttentionCategory.DataValueField = "RT_ID";
            DDLAttentionCategory.DataSource = list;
            DDLAttentionCategory.DataBind();

            DDL_AttentionType.DataTextField = "RT_Name";
            DDL_AttentionType.DataValueField = "RT_ID";
            DDL_AttentionType.DataSource = list;
            DDL_AttentionType.DataBind();
        }
        else
        {
            lblError.Text = ret;
        }
        DDLAttentionCategory.Items.Insert(0, new ListItem("请选择来电类型", "0"));
        DDL_AttentionType.Items.Insert(0, new ListItem("请选择来电类型", "0"));
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void CutPage(object sender, SiteCommon.ChangePageArgs e)
    {
        CutPageBar1.PageIndex = e.PageIndex;
        this.GetPagingCallRecordList();
    }
    #endregion

    #region 来电状态
    public string GetStatus(string value)
    {
        string result = "";
        switch (value)
        {
            case "0":
                result = "当即处理";
                break;
            case "1":
                result = "待处理";
                break;
            case "2":
                result = "待处理完毕";
                break;
        }
        return result;
    }
    #endregion

    #region 更新操作状态
    protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            string id = e.CommandArgument.ToString();
            BLL.PhoneRecordBL pb = new BLL.PhoneRecordBL();
            pb.UpdateStatus(int.Parse(id),DateTime.Now.ToString());
            GetPagingCallRecordList();
        }
        if (e.CommandName == "Look")
        {
            string id = e.CommandArgument.ToString();
            Model.PhoneRecord model = new Model.PhoneRecord();
            model = new BLL.PhoneRecord().GetModel(int.Parse(id));
            if (model != null)
            {
                string url = "CustomerService.aspx?Phone=" + model.CallPhone
                   + "&DNO=" + model.CallPositionNo
                   + "&DID=" + model.CallOrderId
                   + "&CallRecordId=" + model.RD_ID
                   + "&ReturnUrl=CallManage.aspx"
                   + "&AOrderId=a3";

                string script = "<script>top.location.href='" + url + "';</script>";
                Response.Write(script);
                Response.End();
            }
        }
    }
    #endregion

    #region 查找
    protected void Button1_Click(object sender, EventArgs e)
    {
        Hid_Search.Value = "1";
        string DL = "";  //大类
        string phoneno = "";  //记录流水号
        string customerkeyword = "";  //用户关键字
        string attentiontype = "";     //关注分类
        string attentioncontont = "";  //关注分类内容
        string customerservicetype = "";//客服人员类型
        string customerservice = "";//客服人员
        string status = "";//状态
        if (DDL_DL.SelectedIndex > 0)
        {
            DL = " and RD_Type='" + DDL_DL.SelectedValue + "'";
        }
        if (Tbx_NO.Text.Trim().Length > 0)
        {
            phoneno =" and RD_ID="+ Tbx_NO.Text.Trim();
        }
        if (DDL_CustomerAttention.SelectedIndex > 0&&Tbx_AttentionContent.Text.Trim().Length>0)
        {
            customerkeyword = "and " + DDL_CustomerAttention.SelectedValue + " like '%" + Tbx_AttentionContent.Text.Trim() + "%'";
        }
        if (DDL_AttentionType.SelectedIndex > 0)
        {
            attentiontype = "and R.BYZD3='" + DDL_AttentionType.SelectedValue + "'";
        }
        if (Tbx_Attentiontext.Text.Trim().Length > 0)
        {
            attentioncontont = "and R.BYZD4 like '%" + Tbx_Attentiontext.Text.Trim() + "%'";
        }
        if (DDL_CustomerSerice.SelectedIndex > 0)
        {
            customerservicetype = " and R.BYZD2 =" + DDL_CustomerSerice.SelectedValue;
        }
        if (Tbx_Service.Text.Trim().Length > 0)
        {
            customerservice = " and RD_OPERATE='" + Tbx_Service.Text.Trim() + "'";
        }
        if (DDL_Status.SelectedIndex > 0)
        {
            status = " and R.BYZD5='" + DDL_Status.SelectedValue + "'";
        }
        string condition = "";
        condition = DL + phoneno + customerkeyword + attentiontype + attentioncontont + customerservicetype + customerservice + status;
        List<Model.PhoneRecord> list = null;
        int recordCount = 0;

        BLL.PhoneRecordBL mInter = new BLL.PhoneRecordBL();
        string ret = mInter.GetPagingRecordSearchList(CutPageBar1.PageIndex,
            CutPageBar1.PageSize,
            condition,
            out list,
            out recordCount);

        CutPageBar1.RecordCount = recordCount;

        if (ret == Model.Config.SUCCESS)
        {
            rpList.Visible = true;
            rpList.DataSource = list;
            rpList.DataBind();
        }
        else
        {
            rpList.Visible = false;
            lblError.Text = ret;
        }
    }
    #endregion
}
